package com.veo.controller;

import com.veo.entity.BlogQuery;
import com.veo.entity.Tag;
import com.veo.entity.Type;
import com.veo.service.BlogService;
import com.veo.service.TagService;
import com.veo.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

@Controller
public class TagShowController {

    @Autowired
    private TagService tagService;

    @Autowired
    private BlogService blogService;

    @GetMapping("/tags/{id}")
    public String types(@PathVariable(name = "id")Long id, @PageableDefault(size = 8, sort = {"updatedTime"}, direction = Sort.Direction.DESC) Pageable pageable, Model model){

        List<Tag> tags = tagService.listTagTop(10000);
        /*首页跳转时id为-1默认查询第一页*/
        if (id == -1){
            id = tags.get(0).getId();
        }

        model.addAttribute("tags",tags);
        model.addAttribute("page",blogService.listBlog(id,pageable));
        model.addAttribute("activeTagId",id);
        return "tags";
    }
}
